package com.luo.demo.sharding.jdbc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luo.demo.sharding.jdbc.mapper.OrderMapper;
import com.luo.demo.sharding.jdbc.model.dto.OrderInfoDto;
import com.luo.demo.sharding.jdbc.model.entity.Order;
import com.luo.demo.sharding.jdbc.model.entity.OrderItem;
import com.luo.demo.sharding.jdbc.model.entity.Product;
import com.luo.demo.sharding.jdbc.model.param.ProductListParam;
import com.luo.demo.sharding.jdbc.model.param.ProductPageParam;
import com.luo.demo.sharding.jdbc.model.result.CommonResult;
import com.luo.demo.sharding.jdbc.service.IOrderItemService;
import com.luo.demo.sharding.jdbc.service.IOrderService;
import com.luo.demo.sharding.jdbc.service.IProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * <p>
 * 订单信息 服务实现类
 * </p>
 *
 * @author luohq
 * @since 2021-10-22
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    public static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Resource
    private IOrderItemService orderItemService;
    @Resource
    private IProductService productService;


    @Override
    public void addOrders() {
        /** 基础数据 */
        //当前时间
        Date now = new Date();
        //操作用户
        String curUser = "luo";
        //商品价格
        BigDecimal[] productPriceArray = {
                new BigDecimal(100.00),
                new BigDecimal(200.00),
                new BigDecimal(300.00),
        };
        //商品总价
        BigDecimal sumProductPrice = Stream.of(productPriceArray).reduce(BigDecimal.ZERO, (p1, p2) -> p1.add(p2));


        /** 新增3个商品 */
        List<Product> productList = Stream.of(productPriceArray).map(productPrice -> {
            Product product = new Product();
            product.setProductName("商品".concat(productPrice.toString()));
            product.setProductPrice(productPrice);
            product.setCreateTime(now);
            product.setUpdateTime(now);
            return product;
        }).collect(Collectors.toList());
        logger.info("save product batch, param: {}", productList);
        Boolean retResult = this.productService.saveBatch(productList);
        logger.info("save product batch, result: {}", retResult);


        /** 新增4个订单及其订单项 */
        IntStream.range(0, 4).forEach(index -> {
            /** 新增订单 */
            Order order = new Order();
            order.setPrice(sumProductPrice);
            order.setCreateTime(now);
            order.setUpdateTime(now);
            order.setCreateBy(curUser);
            order.setUpdateBy(curUser);
            logger.info("save order, param: {}", order);
            Boolean retResultInner = this.save(order);
            logger.info("save order, result: {}", retResultInner);

            /** 新增订单项目 */
            List<OrderItem> orderItemList = productList.stream()
                    .map(product -> {
                        OrderItem orderItem = new OrderItem();
                        orderItem.setOrderId(order.getOrderId());
                        orderItem.setProductId(product.getProductId());
                        orderItem.setProductCount(1);
                        orderItem.setCreateTime(now);
                        orderItem.setUpdateTime(now);
                        return orderItem;
                    }).collect(Collectors.toList());
            logger.info("save orderItem batch, param: {}", orderItemList);
            retResultInner = this.orderItemService.saveBatch(orderItemList);
            logger.info("save orderItem batch, result: {}", retResultInner);
        });
    }

    @Override
    public OrderInfoDto getOrderDetail(Long orderId) {
        /** 查询订单及订单项 */
        logger.info("get order item detail, orderId={}", orderId);
        OrderInfoDto orderInfoDto = this.baseMapper.selectOrderDetail(orderId);
        logger.info("get order item detail, result: {}", orderInfoDto);

        //若订单不存在 或者 订单项不存在，则直接返回
        if (null == orderInfoDto || CollectionUtils.isEmpty(orderInfoDto.getOrderItemList())) {
            return orderInfoDto;
        }

        /** 查询订单商品列表 */
//        ProductListParam productListParam = new ProductListParam();
//        //设置商品ID列表
//        productListParam.setProductIdList(
//                orderInfoDto.getOrderItemList().stream()
//                        .map(OrderItem::getProductId)
//                        .collect(Collectors.toList())
//        );
//        CommonResult<Product> productPageResult = this.productService.findList(productListParam);
//        logger.info("find order product page, result: {}", productPageResult);
//        orderInfoDto.setProductList(productPageResult.getRows());
//        logger.info("get order detail, result: {}", orderInfoDto);

        Product product = this.productService.getById(orderInfoDto.getOrderItemList().get(0).getProductId());
        logger.info("get product by id, result: {}", product);
        return orderInfoDto;
    }

}
